/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ca.biosample.robot.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;

/**
 *
 * @author mvs
 */
public class SourceTubeIcon extends TubeIcon implements MouseListener {

    private final Integer tubeNumber;
    private SampleInfo info;
    private boolean selected;
    
    public SourceTubeIcon(int num) {
        selected = false;
        tubeNumber = num;
        this.setPreferredSize(new Dimension(50,50));
        info = null;
    }
    
    public SourceTubeIcon(int num, SampleInfo _info) {
        selected = false;
        tubeNumber = num;
        this.setPreferredSize(new Dimension(50,50));
        info = _info;
    }
    
    public void setInfo(SampleInfo _info) {
        info = _info;
    }
    
    public SampleInfo getInfo() {
        return info;
    }
    
    @Override
    public void paint(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();
        
        Color bg = paintSampleColor(g, width, height);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 15, 12);
        g.setColor(Color.BLACK);
        g.setFont(new Font("Arial",Font.ROMAN_BASELINE,11));
        String num = tubeNumber.toString();
        if(num.length() == 1) num = "0" + num;
        g.drawString(num, 2, 11);
        if(bg.getRed() + bg.getBlue() + bg.getGreen() <= 255)
            g.setColor(Color.WHITE);
        if(info != null)
            g.drawString(info.getID(), 2, height - 2);
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, width-1, height-1);
    }
    
    private Color paintSampleColor(Graphics g, int width, int height) {
        Color colorTop, colorBottom;
        if(info == null) {
            colorTop = colorBottom = Color.LIGHT_GRAY;
        }
        else if(info.getType().equals("Whole Blood")) {
            colorTop = colorBottom = new Color(0x00C00000);
        }
        else if(info.getType().equals("Plasma")) {
            colorTop = Color.WHITE;
            colorBottom = new Color(0x00C00000);
        }
        else if(info.getType().equals("White Blood")) {
            colorTop = colorBottom = new Color(0x00FF8080);
        }
        else if(info.getType().equals("Urine")) {
            colorTop = colorBottom = new Color(0x00FFFF70);
        }
        else if(info.getType().equals("RNA")) {
            colorTop = colorBottom = new Color(0x00500000);
        }
        else if(info.getType().equals("Syrum")) {
            colorTop = new Color(0x00CCCCA0);
            colorBottom = new Color(0x00AAAA70);
        }
        else {
            colorTop = colorBottom = Color.LIGHT_GRAY;
        }
        g.setColor(colorTop);
        g.fillRect(0, 0, width - 1, height / 2 );
        g.setColor(colorBottom);
        g.fillRect(0, height / 2, width - 1, height - 1 );
        return colorBottom;
    }
    
    @Override
    public String toString() {
        return info.getID() + "\n" + info.getStudy() + "\n" +
                info.getType();
    }

    public void mouseClicked(MouseEvent arg0) {

    }

    public void mousePressed(MouseEvent arg0) {

    }

    public void mouseReleased(MouseEvent arg0) {

    }

    public void mouseEntered(MouseEvent arg0) {
        System.out.println("Entered: " + toString());
    }

    public void mouseExited(MouseEvent arg0) {
        System.out.println("Exited: " + toString());
    }
}
